#include "requests.h"

//封装get请求
QString Requests::get(QString url)
{
    request.setUrl(QUrl(url));
    //发送请求
    QNetworkReply *reply = manager->get(request);
    //设置事件循环

    QEventLoop eventLoop;
    QObject::connect(manager, &QNetworkAccessManager::finished,
                     &eventLoop, &QEventLoop::quit);
    eventLoop.exec();

    QByteArray bytes = reply->readAll();
    QString response=bytes;
    return response;
}

//封装post请求
QString Requests::post(QString url, QByteArray data)
{
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    request.setHeader(QNetworkRequest::ContentLengthHeader,data.size());
    //发送请求
    QNetworkReply *reply = manager->post(request, data);
    //设置事件循环
    QEventLoop eventLoop;
    QObject::connect(manager, &QNetworkAccessManager::finished,
                     &eventLoop, &QEventLoop::quit);
    eventLoop.exec();

    QByteArray bytes = reply->readAll();
    QString response=bytes;
    return response;
}

Requests::Requests()
{

}

QString Requests::test()
{
    QByteArray pdata;
    pdata.append("username=svdvsdv5156");
    pdata.append("&pwd=ascascascas");

    QString response=this->post(baseUrl+"test",pdata);
    qDebug()<<response;
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());
    QString data =jsonResult.object().value("msg").toString();

    return data;
}

//[POST]注册
QString Requests::signup(QString username, QString pwd)
{
    int uid=0;
    QByteArray pdata;
    pdata.append("username="+username);
    pdata.append("&pwd="+pwd);

    QString response=this->post(baseUrl+"signup",pdata);
    qDebug()<<response;
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());
    QString msg =jsonResult.object().value("msg").toString();
    uid = jsonResult.object().value("uid").toInt();

    return msg;
}

//[GET]登录
UserInfo Requests::login(QString username, QString pwd)
{
    UserInfo userinfo;

    QString pdata;
    pdata.append("?username="+username);
    pdata.append("&pwd="+pwd);

    QString response=this->get(baseUrl+"login"+pdata);
    qDebug()<<response;
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());
    userinfo.msg =jsonResult.object().value("msg").toString();
    userinfo.uid = jsonResult.object().value("uid").toInt();
    userinfo.type = jsonResult.object().value("type").toInt();
    userinfo.last_planid = jsonResult.object().value("last_planid").toInt();
    userinfo.ok = jsonResult.object().value("ok").toInt();
    return userinfo;
}

//[GET]下载当天计划
vector<WordBook> *Requests::loadplan(int pid)
{
    vector<WordBook>* wordlist =new vector<WordBook>;

    QString pdata;
    pdata.append("?pid="+QString::number(pid));
    QString response=this->get(baseUrl+"loadplan"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[loadplan]ok: "<<ok;
    if(ok==0){  //错误返回空
        return wordlist;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        WordBook wb;
        wb.word = item.value("word").toString();
        wb.translation =item.value("translation").toString();
        wb.sentence = item.value("sentence").toString();
        wordlist->push_back(wb);
    }
    return wordlist;
}

//[GET]新增计划
int Requests::addplan(int uid, int bid, int everyday_num)
{
    int pid = 0;

    QString pdata;
    pdata.append("?uid="+QString::number(uid));
    pdata.append("&bid="+QString::number(bid));
    pdata.append("&everyday_num="+QString::number(everyday_num));
    QString response = this->get(baseUrl+"addplan"+pdata);
    QJsonDocument jsonResult = QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    QString msg =jsonResult.object().value("msg").toString();
    qDebug()<<"ok: "<<ok;
    if(ok==0){  //错误返回0
        return pid;
    }
    pid = jsonResult.object().value("pid").toInt();

    return pid;
}

//[GET]获取计划列表
vector<Plan> *Requests::getplanlist(int uid)
{
    vector<Plan> *plan = new vector<Plan>;

    QString pdata;
    pdata.append("?uid="+QString::number(uid));
    QString response=this->get(baseUrl+"getplanlist"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"ok: "<<ok;
    if(ok==0){  //错误返回空
        return plan;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Plan tempPlan;
        tempPlan.pid = item.value("pid").toInt();
        tempPlan.bookname =item.value("bookname").toString();
        tempPlan.create_time = item.value("create_time").toString();
        tempPlan.edit_time = item.value("edit_time").toString();
        tempPlan.total_num = item.value("total_num").toInt();
        tempPlan.remain_num = item.value("remain_num").toInt();
        tempPlan.everyday_num = item.value("everyday_num").toInt();
        tempPlan.day_remain_num = item.value("day_remain_num").toInt();
        plan->push_back(tempPlan);
    }

    return plan;
}

//[GET]修改计划
int Requests::updateplan(int pid, int everyday_num)
{
    int ok = 0;

    QString pdata;
    pdata.append("?pid="+QString::number(pid));
    pdata.append("&everyday_num="+QString::number(everyday_num));
    QString response = this->get(baseUrl+"updateplan"+pdata);
    QJsonDocument jsonResult = QJsonDocument::fromJson(response.toUtf8());

    ok =jsonResult.object().value("ok").toInt();

    qDebug()<<"ok: "<<ok;
    if(ok==0){  //错误返回0
        return ok;
    }
    return ok;
}

//[GET]同步学习进度
int Requests::synclearning(int pid, QString word, int ignore)
{
    int ok = 0;

    QString pdata;
    pdata.append("?pid="+QString::number(pid));
    pdata.append("&word="+word);
    pdata.append("&ignore="+QString::number(ignore));
    QString response = this->get(baseUrl+"synclearning"+pdata);
    QJsonDocument jsonResult = QJsonDocument::fromJson(response.toUtf8());

    ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"ok: "<<ok;
    if(ok==0){  //错误返回0
        return ok;
    }
    return ok;
}

//[GET]获取词书列表
vector<Book> *Requests::getbooklist()
{
    vector<Book> *booklist = new vector<Book>;

    QString response=this->get(baseUrl+"getbooklist");
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[getbooklist] ok: "<<ok;
    if(ok==0){  //错误返回空
        return booklist;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Book tempbook;
        tempbook.bid = item.value("bid").toInt();
        tempbook.bookname =item.value("bookname").toString();
        tempbook.total_num = item.value("total_num").toInt();
        tempbook.creator = item.value("creator").toInt();
        booklist->push_back(tempbook);
    }

    return booklist;
}

//[POST]新增习题
int Requests::addexercise(Exercise &exercise)
{
    int exid=0;
    QByteArray pdata;
    pdata.append("word="+exercise.word);
    pdata.append("&question="+exercise.question);
    pdata.append("&choice_A="+exercise.choice_A);
    pdata.append("&choice_B="+exercise.choice_B);
    pdata.append("&choice_C="+exercise.choice_C);
    pdata.append("&choice_D="+exercise.choice_D);
    pdata.append("&answer="+exercise.answer);
    pdata.append("&creator="+QString::number(exercise.creator));

    QString response=this->post(baseUrl+"addexercise",pdata);
    qDebug()<<response;
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());
    exid = jsonResult.object().value("exid").toInt();

    return exid;
}

//[POST]新增笔记
int Requests::addnote(Note &note)
{
    int nid=0;
    QByteArray pdata;
    pdata.append("word="+note.word);
    pdata.append("&content="+note.content);
    pdata.append("&creator="+QString::number(note.creator));

    QString response=this->post(baseUrl+"addnote",pdata);
    qDebug()<<response;
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());
    nid = jsonResult.object().value("nid").toInt();

    return nid;
}

//[POST]修改笔记
int Requests::updatenote(Note &note)
{
    QByteArray pdata;
    pdata.append("content="+note.content);
    pdata.append("&nid="+QString::number(note.nid));

    QString response=this->post(baseUrl+"updatenote",pdata);
    qDebug()<<response;
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[updatenote] ok: "<<ok;
    return ok;
}

//[GET]按用户获取笔记
vector<Note> *Requests::getnotebyuser(int uid,QString word)
{
    vector<Note> *notes = new vector<Note>;
    QString pdata;
    pdata ="?uid="+QString::number(uid);
    QString response=this->get(baseUrl+"getnotebyuser"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[getnotebyuser] ok: "<<ok;
    if(ok==0){  //错误返回空
        return notes;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        if(item.value("word").toString().compare(word)!=0)  //筛选出该单词的笔记
            continue;
        Note tempnote;
        tempnote.nid = item.value("nid").toInt();
        tempnote.word =item.value("word").toString();
        tempnote.content = item.value("content").toString();
        tempnote.create_time = item.value("create_time").toString();
        tempnote.edit_time = item.value("edit_time").toString();
        tempnote.approve = item.value("approve").toInt();
        notes->push_back(tempnote);
    }

    return notes;
}

//[GET]按单词获取笔记
vector<Note> *Requests::getnotebyword(QString word)
{
    vector<Note> *notes = new vector<Note>;
    QString pdata;
    pdata ="?word="+word;
    QString response=this->get(baseUrl+"getnotebyword"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[getnotebyword] ok: "<<ok;
    if(ok==0){  //错误返回空
        return notes;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Note tempnote;
        tempnote.nid = item.value("nid").toInt();
        tempnote.word =item.value("word").toString();
        tempnote.content = item.value("content").toString();
        tempnote.creator = item.value("creator").toInt();
        tempnote.username = item.value("username").toString();
        tempnote.create_time = item.value("create_time").toString();
        tempnote.edit_time = item.value("edit_time").toString();
        tempnote.approve = item.value("approve").toInt();
        notes->push_back(tempnote);
    }

    return notes;
}

//[GET]审核笔记
int Requests::checknote(int nid, int approve)
{
    int ok = 0;

    QString pdata;
    pdata.append("?nid="+QString::number(nid));
    pdata.append("&approve="+QString::number(approve));
    QString response = this->get(baseUrl+"checknote"+pdata);
    QJsonDocument jsonResult = QJsonDocument::fromJson(response.toUtf8());

    ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[checknote]ok: "<<ok;
    if(ok==0){  //错误返回0
        return ok;
    }
    return ok;
}

//[GET]审核习题
int Requests::checkexercise(int exid, int approve)
{
    int ok = 0;

    QString pdata;
    pdata.append("?exid="+QString::number(exid));
    pdata.append("&approve="+QString::number(approve));
    QString response = this->get(baseUrl+"checkexercise"+pdata);
    QJsonDocument jsonResult = QJsonDocument::fromJson(response.toUtf8());

    ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[checkexercise]ok: "<<ok;
    if(ok==0){  //错误返回0
        return ok;
    }
    return ok;
}

//[GET]获取习题
vector<Exercise> *Requests::loadexercise(int pid)
{
    vector<Exercise> *exercises = new vector<Exercise>;

    QString pdata;
    pdata.append("?pid="+QString::number(pid));

    QString response=this->get(baseUrl+"loadexercise"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[loadexercise] ok: "<<ok;
    if(ok==0){  //错误返回空
        return exercises;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Exercise tempnote;
        tempnote.exid = item.value("exid").toInt();
        tempnote.word =item.value("word").toString();
        tempnote.question =item.value("question").toString();
        tempnote.answer =item.value("answer").toString();
        tempnote.choice_A =item.value("choice_A").toString();
        tempnote.choice_B =item.value("choice_B").toString();
        tempnote.choice_C =item.value("choice_C").toString();
        tempnote.choice_D =item.value("choice_D").toString();
        tempnote.creator = item.value("creator").toInt();
        tempnote.username = item.value("username").toString();
        tempnote.create_time = item.value("create_time").toString();
        tempnote.edit_time = item.value("edit_time").toString();
        tempnote.approve = item.value("approve").toInt();
        exercises->push_back(tempnote);
    }

    return exercises;
}

//[GET]获取未审核习题
vector<Exercise> *Requests::getuncheckedexercise()
{
    vector<Exercise> *exercises = new vector<Exercise>;
    QString response=this->get(baseUrl+"getuncheckedexercise");
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[getuncheckedexercise] ok: "<<ok;
    if(ok==0){  //错误返回空
        return exercises;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Exercise tempnote;
        tempnote.exid = item.value("exid").toInt();
        tempnote.word =item.value("word").toString();
        tempnote.question =item.value("question").toString();
        tempnote.answer =item.value("answer").toString();
        tempnote.choice_A =item.value("choice_A").toString();
        tempnote.choice_B =item.value("choice_B").toString();
        tempnote.choice_C =item.value("choice_C").toString();
        tempnote.choice_D =item.value("choice_D").toString();
        tempnote.creator = item.value("creator").toInt();
        tempnote.username = item.value("username").toString();
        tempnote.create_time = item.value("create_time").toString();
        tempnote.edit_time = item.value("edit_time").toString();
        tempnote.approve = item.value("approve").toInt();
        exercises->push_back(tempnote);
    }

    return exercises;
}

//[GET]获取未审核笔记
vector<Note> *Requests::getuncheckednote()
{
    vector<Note> *notes = new vector<Note>;
    QString response=this->get(baseUrl+"getuncheckednote");
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[getuncheckednote] ok: "<<ok;
    if(ok==0){  //错误返回空
        return notes;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Note tempnote;
        tempnote.nid = item.value("nid").toInt();
        tempnote.word =item.value("word").toString();
        tempnote.content = item.value("content").toString();
        tempnote.creator = item.value("creator").toInt();
        tempnote.username = item.value("username").toString();
        tempnote.create_time = item.value("create_time").toString();
        tempnote.edit_time = item.value("edit_time").toString();
        tempnote.approve = item.value("approve").toInt();
        notes->push_back(tempnote);
    }

    return notes;
}

//[GET]获取打卡记录
vector<int> *Requests::checkinrecord(int pid)
{
    vector<int> *record = new vector<int>;
    QString pdata;
    pdata.append("?pid="+QString::number(pid));

    QString response=this->get(baseUrl+"checkinrecord"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[checkinrecord] ok: "<<ok;
    if(ok==0){  //错误返回空
        return record;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    qDebug()<<"[checkinrecord] data: "<<data;
    foreach (QJsonValue line, data){
        qDebug()<<line.toString().toInt();
        record->push_back(line.toString().toInt());
    }

    return record;
}

//[GET]下载复习计划
vector<WordBook> *Requests::loadreview(int pid)
{
    vector<WordBook>* wordlist =new vector<WordBook>;

    QString pdata;
    pdata.append("?pid="+QString::number(pid));
    QString response=this->get(baseUrl+"loadreview"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"[loadreview]ok: "<<ok;
    if(ok==0){  //错误返回空
        return wordlist;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        WordBook wb;
        wb.word = item.value("word").toString();
        wb.translation =item.value("translation").toString();
        wordlist->push_back(wb);
    }
    return wordlist;
}

//[GET]获取计划详细内容
Plan Requests::getplaninfo(int pid)
{
    Plan plan;

    QString pdata;
    pdata.append("?pid="+QString::number(pid));
    QString response=this->get(baseUrl+"getplaninfo"+pdata);
    QJsonDocument jsonResult =QJsonDocument::fromJson(response.toUtf8());

    int ok =jsonResult.object().value("ok").toInt();
    qDebug()<<"ok: "<<ok;
    if(ok==0){  //错误返回空
        return plan;
    }

    QJsonArray data = jsonResult.object().value("data").toArray();
    foreach (QJsonValue line, data) {
        QJsonObject item = line.toObject();
        Plan tempPlan;
        plan.pid = item.value("pid").toInt();
        plan.bookname =item.value("bookname").toString();
        plan.create_time = item.value("create_time").toString();
        plan.edit_time = item.value("edit_time").toString();
        plan.total_num = item.value("total_num").toInt();
        plan.remain_num = item.value("remain_num").toInt();
        plan.everyday_num = item.value("everyday_num").toInt();
        plan.day_remain_num = item.value("day_remain_num").toInt();
    }

    return plan;
}
